package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.BeautifulTeacherBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.BeautifulTeacherPresenter;
import com.android.infantschool.model.mvp.presenter.BeautifulTeacherPresenterImpl;
import com.android.infantschool.model.mvp.view.BeautifulTeacherView;
import com.android.infantschool.ui.adapter.BeautifulTeacherAdapter;
import com.android.infantschool.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\5 0005 18:30
 * @class describe
 */
public class BeautifulTeacherActivity extends BaseActivity implements BeautifulTeacherAdapter.OnLister, BeautifulTeacherView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.rl_beautiful_teacher)
    public RelativeLayout rlBeautifulTeacher;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;

    private BeautifulTeacherAdapter adapter;

    private BeautifulTeacherPresenter teacherPresenter = null;

    public static Intent newIntent(Activity activity) {
        Intent intent = new Intent(activity, BeautifulTeacherActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_beautiful_teacher);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        teacherPresenter = new BeautifulTeacherPresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText("最美教师");

        adapter = new BeautifulTeacherAdapter(this, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(adapter);

        this.showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        teacherPresenter.getBeautifulTeacher(map);

        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void errorBeautifulTeacher(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successBeautifulTeacher(BaseBean baseBean) {
        this.dismissLoading();
        BeautifulTeacherBean teacherBean = (BeautifulTeacherBean) baseBean;
        if (teacherBean.getCode() == 0) {
            adapter.setData(teacherBean.getData());
        } else {
            ToastUtil.showShortToast(this, "获取失败:" + teacherBean.getMsg());
        }
    }

    @Override
    public void lister(int position, int type) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter.clear();
        if (teacherPresenter != null) {
            teacherPresenter.onDestroy();
            teacherPresenter = null;
        }
    }


}